home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / ODMemMgr / Sources / MemDebgM.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  3.6 KB  |  156 lines  |  [TEXT/MPS ]

  1. /*
  2. //SRF - UNMODIFIED
  3.     File:        MemDebgM.cpp
  4.  
  5.     Contains:    Mac-specific memory management debug routines
  6.  
  7.     Owned by:    Jens Alfke, Vincent Lo, Nick Pilch, Michael Burbidge
  8.  
  9.     Copyright:    © 1993 - 1995-96 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.                   3/5/96    TWB        Comment out unused arguments to suppress warnings. 
  14.          <5>      5/4/95    jpa        Abstracted out highest/lowest possible
  15.                                     address stuff [1246077]
  16.          <4>     1/12/95    jpa        Include LowMem.h [1210936]
  17.          <3>    10/24/94    jpa        Call StripAddress in BasicValidatePtr.
  18.                                     [1193659]
  19.          <2>     9/29/94    RA        1189812: Mods for 68K build.
  20.          <1>     9/14/94    jpa        first checked in
  21.     
  22.     In Progress:
  23. */
  24.  
  25.  
  26. #ifndef _MEMCNFIG_
  27. #include "MemCnfig.h"
  28. #endif
  29.  
  30.  
  31. #if MM_DEBUG
  32.  
  33.  
  34. #ifndef _MEMDEBG_
  35. #include "MemDebg.h"
  36. #endif
  37.  
  38. #ifndef _MEMMGRPV_
  39. #include "MemMgrPv.h"
  40. #endif
  41.  
  42. #ifndef _MEMHOOKS_
  43. #include "MemHooks.h"
  44. #endif
  45.  
  46. #ifndef __MEMORY__
  47. #include <Memory.h>        // Mac memory manager
  48. #endif
  49.  
  50. #ifndef __LOWMEM__
  51. #include <LowMem.h>
  52. #endif
  53.  
  54.  
  55. const size_t kMaxHandleBlockSize = 0x7FFFFFFF;
  56.  
  57.  
  58. //------------------------------------------------------------------------------
  59. // PtrInHeap
  60. //------------------------------------------------------------------------------
  61.  
  62. inline static MMBoolean
  63. PtrInHeap( const void *p, THz heap )
  64. {
  65.     return (p >= &heap->heapData) && (p < heap->bkLim);
  66. }
  67.  
  68.  
  69. //------------------------------------------------------------------------------
  70. // BasicValidatePtr
  71. //------------------------------------------------------------------------------
  72.  
  73. const char*
  74. BasicValidatePtr( const void *p, MMBoolean /*mustBeInHeap*/ )
  75. {
  76.     // If we could be sure that p were in the app zone, we could be more
  77.     // stringent. But it might be in the System heap or in temp-mem.
  78.     
  79. #if 0
  80.     static THz tempZone = kMMNULL;
  81.     if (tempZone == kMMNULL)
  82.     {
  83.         // Find the temp zone by allocating a handle in it and then getting its zone:
  84.         OSErr err;
  85.         
  86.         Handle handle = ::TempNewHandle(2, &err);
  87.         MM_ASSERT(err == noErr && handle != kMMNULL);
  88.         
  89.         tempZone = ::HandleZone(handle);
  90.         MM_ASSERT(tempZone != kMMNULL);
  91.         
  92.         ::DisposeHandle(handle);
  93.     }
  94. #endif
  95.  
  96.     p = StripAddress((void*)p);
  97.     
  98.     if( p == kMMNULL )
  99.         return "is NULL";
  100. //    else if( (MMULong)p & 1 )
  101. //        return "is odd";
  102.     else if( (MMULong)p & 2 )
  103.         return "isn't 4-byte-aligned";
  104. #if 0
  105.     // This does not appear to work any longer with the Modern Memory Manager...
  106.     else if( mustBeInHeap && !PtrInHeap(p,ApplicationZone())
  107.                             && !PtrInHeap(p,SystemZone())
  108.                               && !PtrInHeap(p,tempZone) )
  109.         return "is not in a known Mac heap zone";
  110. #endif
  111.     else if( /*!mustBeInHeap &&*/ (p<LOWEST_POSSIBLE_ADDRESS() || p>HIGHEST_POSSIBLE_ADDRESS()) )
  112.         return "is outside the known universe";
  113.     else
  114.         return kMMNULL;
  115. }
  116.  
  117.  
  118. //------------------------------------------------------------------------------
  119. // MMValidateHandle
  120. //------------------------------------------------------------------------------
  121.  
  122. MMBoolean
  123. MMValidateHandle( MMHandle MMHandle )
  124. {
  125.     Handle h = (Handle) MMHandle;
  126.     
  127.     // Check handle itself as a pointer:
  128.     const char *err = BasicValidatePtr(h);
  129.     
  130.     if( err == kMMNULL && *h == kMMNULL )
  131.         err = "is purged";
  132.     
  133.     if( err ) {
  134.         MM_WARN("Handle %p %s!",h,err);
  135.         return kMMFalse;
  136.     }
  137.     
  138.     // Check master pointer for validity:
  139.     err = BasicValidatePtr(*h);
  140.     if( err ) {
  141.         MM_WARN("Handle %p's master %p %s!",h,*h,err);
  142.         return kMMFalse;
  143.     }
  144.     
  145.     // Check handle's size:
  146.     size_t size = GetHandleSize(h);
  147.     if( size > kMaxHandleBlockSize )
  148.         MM_WARN("Handle %p has bogus size %08lx!",h,size);
  149.     else if( MemError() != noErr )
  150.         MM_WARN("Handle %p caused err %d on GetHandleSize",h,MemError());
  151.     else
  152.         return kMMTrue;
  153.     return kMMFalse;
  154. }
  155.  
  156. #endif /*MM_DEBUG*/